package com.hzhc.controller.chargemanager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.ChargeContract;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.ChargeContractFileService;
import com.hzhc.service.ChargeContractService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.ChargeContractVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/5/28 16:36
 * @description ChargeContractController
 */
@RestController
@RequestMapping("/chargeContract")
@RequiredArgsConstructor
public class ChargeContractController extends BaseController {

    private final ChargeContractService contractService;
    private final ChargeContractFileService fileService;

    @Value("${file.contractFileUploadFolder}")
    private String contractFileUploadFolder;
    @Value("${file.contractFileAccessPath}")
    private String contractFileAccessPath;

    /**
     * 计费合同 list
     * @param vo
     * @return
     */
    @RequestMapping("/getChargeContractList")
    @ResponseBody
    public ResultUtil<List<ChargeContractVo>> getChargeContractList(@RequestBody ChargeContractVo vo){
        Page<ChargeContractVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<ChargeContractVo> list = contractService.getChargeContractList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 计费合同 add
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/addChargeContract")
    @SysTemLog(operModul = "计费管理-计费合同",operType = "ADD",operDesc = "计费合同添加",modular="PC")
    public ResultUtil<T> addChargeContract(HttpServletRequest request, ChargeContractVo vo,
                                           @RequestParam(value = "fileRef",required = false) List<MultipartFile> fileRef){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        vo.setCreateTime(LocalDateTime.now());
        vo.setFile(fileRef);
        return contractService.addChargeContract(vo,contractFileUploadFolder,contractFileAccessPath);
    }

    /**
     * 合同编号 check
     * @param contract
     * @return
     */
    @PostMapping("/checkContractNo")
    public ResultUtil<T> checkContractNo(@RequestBody ChargeContract contract){
        Boolean b = contractService.checkContractNo(contract);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.build(ResultUtilCodeEnum.CONTRACT_EXIST_ERROR);
    }

    /**
     * 计费合同 update
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/updateChargeContract")
    @SysTemLog(operModul = "计费管理-计费合同",operType = "UPDATE",operDesc = "计费合同修改",modular="PC")
    public ResultUtil<T> updateChargeContract(HttpServletRequest request, ChargeContractVo vo,
                                              @RequestParam(value = "fileRef",required = false) List<MultipartFile> fileRef){
        Long userID = TokenUtil.getUserID(request);
        vo.setUpdateUserId(userID);
        vo.setUpdateTime(LocalDateTime.now());
        vo.setFile(fileRef);
        return contractService.updateChargeContract(vo,contractFileUploadFolder,contractFileAccessPath);
    }

    /**
     * 计费合同 delete
     * @param id
     * @return
     */
    @GetMapping("/delChargeContract/{id}")
    @SysTemLog(operModul = "计费管理-计费合同",operType = "DELETE",operDesc = "计费合同删除",modular="PC")
    public ResultUtil<T> delChargeContract(@PathVariable Long id){
        return contractService.delChargeContract(id);
    }

    /**
     * 附件删除
     * @param id
     * @return
     */
    @GetMapping("/delContractById/{id}")
    public ResultUtil<T> delContractById(@PathVariable Long id){
        return fileService.delContractById(id);
    }

}
